//
//  NoticeView.m
//  MyMovie
//
//  Created by Weiwei on 16/3/5.
//  Copyright © 2016年 bever贝沃汇力 weiwei. All rights reserved.
//

#import "NoticeView.h"
#import "NoticeCell.h"
#import "NoticeModel.h"

@interface NoticeView (){
    UITableView* _tableView;
    UIImageView* _imageView;
}

@end


@implementation NoticeView


#pragma -mark 步骤一 创建tableView实现代理
-(id)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"home_top_movie_background_cover"]]];
        
        
        
        //创建一个tableView
        [self createTableView];
        
    }
    
    return self;
}

-(void)createTableView{
    
    //1. 创建tableVIEW
     _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height - 64 -49) style:UITableViewStylePlain];
    
    _tableView.backgroundColor = [UIColor clearColor];
    
    //2.实现代理
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [self addSubview:_tableView];
    
    

    
    
}

#pragma -mark 保证拿到数据后，再创建头试图
-(void)setDataList:(NSArray *)dataList{
    _dataList = dataList;
    
    
#pragma -mark 创建一个view作为头试图
    //由于headeView 无法拉伸变宽，所以我们改变的是ta上面的imageview的大小
    //1.创建headerView
    UIView* _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.width, 200)];
    _headerView.backgroundColor = [UIColor redColor];
    _tableView.tableHeaderView = _headerView;
    
    //2.创建一个imageView
    _imageView = [[UIImageView alloc]initWithFrame:_headerView.bounds];
    _imageView.backgroundColor = [UIColor yellowColor];
    
    //获得第一个model
    NoticeModel* model = _dataList[0];
    [_imageView setImageWithURL:[NSURL URLWithString:model.coverImg]];
    [_headerView addSubview:_imageView];
    
    
    //创建一个lable加到imageView上
    UILabel* _titleLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 170, self.width, 30)];
    _titleLable.textAlignment = NSTextAlignmentCenter;
    _titleLable.textColor = [UIColor whiteColor];
    _titleLable.font = [UIFont systemFontOfSize:20];
    
    _titleLable.text = model.movieName;
    [_imageView addSubview:_titleLable];
    

}


#pragma -mark 计算滑动时的偏移量算法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
//    NSLog(@"%.1f",scrollView.contentOffset.y);
    
    
    //1.获得当前的偏移;
    CGFloat offsetY  =  scrollView.contentOffset.y;
    
    if (offsetY < 0) {
        
        //2.计算放大的倍数比例
        //平移量
        float distance = -offsetY;
        //计算放大的比例倍数
        float scale = (distance + 200)/200;
        
        _imageView.transform = CGAffineTransformMakeScale(scale, scale);
        
//        _imageView.transform = CGAffineTransformScale(_imageView.transform, scale, scale);
        
        //重新设置imageView的放大后的位置
        //y坐标变成偏移值
        _imageView.top = offsetY;
        //x
        _imageView.left = -(414* scale - 414)/2;
        
        
    }
    
    
    
    
}








//代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
//    return 10;
    
    return _dataList.count;
    
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
   //1.使用复用池
    NoticeCell* cell = [tableView dequeueReusableCellWithIdentifier:@"notice_cell"];
   //2.
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"NoticeCell" owner:nil options:nil]lastObject];
    }
    
   //3.传递
    //取出当前的model
     NoticeModel* model  = _dataList[indexPath.row];
    cell.model = model;
    
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 100;
}

@end
